package com.rslai.base.tool.servertest.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertyUtils {

    static Map<String, String> configs = new HashMap();
    static final Logger LOGGER = LoggerFactory.getLogger(PropertyUtils.class);

    static {
        loadProperties("servertest.properties");
    }

    private static void loadProperties(String propertiesFileName) {
        try {
            Properties PROPERTIES = new Properties();

            InputStream stream = PropertyUtils.class.getClassLoader().getResourceAsStream(propertiesFileName);
            if (stream == null) return;

            PROPERTIES.load(stream);
            Set<Map.Entry<Object, Object>> entries = PROPERTIES.entrySet();
            for (Map.Entry entry : entries) {
                String key = entry.getKey() == null ? null : StringUtils.trim(entry.getKey().toString());
                String value = entry.getValue() == null ? null : StringUtils.trim(entry.getValue().toString());
                configs.put(new String(key.getBytes("ISO8859-1"), "UTF-8"), new String(value.getBytes("ISO8859-1"), "UTF-8"));
            }
        } catch (IOException e) {
            LOGGER.error("load properties file has error,", e);
        }
    }

    public static String getProperty(String name) {
        return (String) configs.get(name);
    }

    public static String getProperty(String name, String defaultValue) {
        String result = (String) configs.get(name);
        if (StringUtils.isBlank(result)) {
            result = defaultValue;
        }
        return result;
    }

    public static String replaceConfigValue(String source) {
        if (StringUtils.isNotBlank(source)) {
            for (Map.Entry entry : configs.entrySet()) {
                source = StringUtils.replace(source, "$" + StringUtils.trim((String) entry.getKey()), (String) entry.getValue());
                source = StringUtils.replace(source, "${" + StringUtils.trim((String) entry.getKey()) + "}", (String) entry.getValue());
            }
        }
        return source;
    }

}
